//
//  YYBasiListApiManager.m
//  优悦一族
//
//  Created by liang on 2017/5/11.
//  Copyright © 2017年 umed. All rights reserved.
//

#import "YYBasiListApiManager.h"
#import "YYHTTPConst.h"

NSString *const koffset = @"offset";
NSString *const klimit = @"limit";
@interface YYBasiListApiManager ()<CTAPIManagerValidator>
@property (nonatomic, assign) BOOL reload;
@end

@implementation YYBasiListApiManager

#pragma mark life-cycle
-(instancetype)init {
    self = [super init];
    if (self) {
        
        self.validator = self;
        self.offset = 0;
        self.limit = 10;
    }
    
    return self;
}
#pragma mark -APIManager

-(NSString *)methodName {
    
    NSAssert(0, @"subclass must rewrite this method");
    return nil;
}

-(NSString *)serviceType {
    
    return kCTServiceAuthonticationYY;
}

-(CTAPIManagerRequestType)requestType {
    
    return CTAPIManagerRequestTypeGet;
}

-(BOOL)shouldCache {
    
    return NO;
}
-(NSDictionary *)reformParams:(NSDictionary *)params {
    
    if (self.isLoadMore) {
        
        self.offset = self.offset + self.limit;
        NSNumber *offset = @(self.offset);
        NSNumber *limit = @(self.limit);
        return @{koffset:offset,klimit:limit};
    }
    
    if (self.isRefesh) {
        
        NSNumber *offset = @(0);
        NSNumber *limit = @(self.limit);
        return @{koffset:offset,klimit:limit};
    }
    
    if (self.reload) {
        
        NSNumber *offset = @(self.offset);
        NSNumber *limit = @(self.limit);
        return @{koffset:offset,klimit:limit};
    }
    NSNumber *offset = @(0);
    NSNumber *limit = @(self.limit);
    return @{koffset:offset,klimit:limit};
    
}

-(NSInteger)loadData {
    
    self.isRefesh = NO;
    self.isLoadMore = NO;
    self.reload = NO;
    return [super loadData];
}

-(void)loadNext {
    
    self.isLoadMore = YES;
    self.isRefesh = NO;
    self.reload = NO;
    [super loadData];
}

-(void)loadLaste {
    
    self.isRefesh = YES;
    self.isLoadMore = NO;
    self.reload = NO;
    [super loadData];
}

- (void)relaodData {
    
    if (self.offset == 0) {
        
        [self loadData];
    } else
        [self loadNext];
}
#pragma mark CTAPIManagerValidator

-(BOOL)manager:(CTAPIBaseManager *)manager isCorrectWithParamsData:(NSDictionary *)data {
    
    return YES;
}

-(BOOL)manager:(CTAPIBaseManager *)manager isCorrectWithCallBackData:(NSDictionary *)data {
    
    NSString *code = data[@"code"];
    if (![code isEqualToString:@"100"]) {
        
        return NO;
    }
    return YES;
}

-(void)afterPerformFailWithResponse:(CTURLResponse *)response {
    
    [super afterPerformFailWithResponse:response];
    if (self.offset != 0 && self.isLoadMore) {
        
        self.offset = self.offset - self.limit;
    }
}

@end
